var app = getApp()
console.log(app.globalData.url) // 获取 
var serverUrl = app.globalData.url;
var getDataURL = serverUrl + "/api/hr/kehuinfo/";
var getDataURLgj = serverUrl + "/api/hr/gengjing/";

var choseRiqi;
var choseRiqi2;
var sousuos = { pageNum: 1 };
var xiangqingId;
var userinfo = {};
var page = 1;
var total=0;
Page({
  data: {
    toView: 'red',
    scrollTop: 700,
    userinfo: {},
    view: 'submit',
    items: [],
    showgengduo: "none",
    xuanzeshijian: null,
    xuanzeshijian2: null,
    page: 1,
    pageSize:10
  },
xiepinglun(){//写评论
},
  upper(e) {
   // console.log(e, "1111");
  },
  lower(e) {
    page++;
   if(page<=Math.ceil(total/this.data.pageSize)) {
    sousuos = { pageNum: page }
    this.getLists(sousuos);
    }
  },
  scroll(e) {
   // console.log(e.detail.scrollTop, "top");
  },
  scrollToTop(e) {
   // console.log(e, "777777");
    this.setData({
      scrollTop: 0,
    });
  },

  onPullDownRefresh() {
    console.log('onPullDownRefresh', new Date());
  },
  stopPullDownRefresh() {
    my.stopPullDownRefresh({
      complete(res) {
        console.log(res, new Date());
      },
    });
  },
  onLoad(query) {
    // 页面加载
    this.getList(1, "")
  },
  searchs() {

  },
  onSubmit(e) {//搜索按钮点击

    var that = this;
    var dat = e.detail.value;
    dat.pageNum = 1;
    this.getLists(dat);
    sousuos = dat;
  },
  onResets() {//重置
    this.setData({
      xuanzeshijian: "",
      xuanzeshijian2: ""
    })
    sousuos = {
      pageNum: 1
    };
  },
  gengduosousuo() {//点击更多搜索
    var isshow;
    if (this.data.showgengduo == "none") {
      isshow = "block"
    } else {
      isshow = "none"
    }
    this.setData({
      showgengduo: isshow
    })
  },
  xiangqing(event) {
    xiangqingId = event.target.dataset.alphaId;
    console.info(xiangqingId)
    dd.navigateTo({
      url: '/page/index/customer/followUp/followUp?xiangqingId=' + xiangqingId + "&fromPage=followHistory"
    })
  },
  shanchu(event) {//删除
    xiangqingId = event.target.dataset.alphaId;
    console.info(xiangqingId)
    var that = this;
    my.confirm({
      title: '请注意',
      content: '确定要删除：' + event.target.dataset.alphaTime + "的记录吗?",
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: result => {
        if (`${result.confirm}` == 'true' || `${result.confirm}` == true) {
          dd.httpRequest({
            url: getDataURLgj + "remove",
            method: 'POST',
            data:
            {
              ids: event.target.dataset.alphaId
            }
            ,
            dataType: 'json',
            success: function (res) {
              if (res.data.code == 0) {
                dd.showToast({
                  content: '删除成功', // 文字内容
                });
                that.setData({
                  items: that.data.items.filter(item => item.id != xiangqingId)
                })
              } else {
                dd.showToast({
                  content: '删除失败', // 文字内容
                });
              }
            },
            fail: function (res) {
              dd.alert({ content: 'fail' });
            },
            complete: function (res) {
              // dd.alert({content: 'complete'});
            }
          });
        }

      },
    });
  },
  gengduosousuoss0() {//弹出日期选择
    dd.datePicker({
      format: 'yyyy-MM-dd',
      currentDate: this.formatDates(new Date()),
      success: (res) => {
        choseRiqi = res.date;
        this.setData({
          xuanzeshijian: res.date
        })
      }
    })
  },
  gengduosousuoss1() {//弹出日期选择
    dd.datePicker({
      format: 'yyyy-MM-dd',
      currentDate: this.formatDates(new Date()),
      success: (res) => {
        choseRiqi2 = res.date;
        this.setData({
          xuanzeshijian2: res.date
        })
      },
    });
  },
  formatDates(date) {//获取当前日期
    var myyear = date.getFullYear();
    var mymonth = date.getMonth() + 1;
    var myweekday = date.getDate();

    if (mymonth < 10) {
      mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
      myweekday = "0" + myweekday;
    }
    return (myyear + "-" + mymonth + "-" + myweekday);
  }
  , getList() {
    this.getLists(sousuos)
  },
  getLists(sousuos) {
    console.info(sousuos.pageNum, ">>>>pagessssssss")
    sousuos.pageSize = this.data.pageSize;
    var that = this;
    dd.httpRequest({
      url: getDataURLgj + "list",
      method: 'POST',
      data: sousuos,
      dataType: 'json',
      success: function (res) {
              total=res.data.total;
        if (sousuos.pageNum == 1) {
          that.setData({
            items: res.data.rows
          })
        } else {  //上拉刷新
         if(that.data.items.length>0){
            var itemss=that.data.items;
            itemss.push(res.data.rows);
            that.setData({
              items: itemss
            })
          }
        }
        if (that.data.userinfo.userid == undefined) {
          dd.getStorage({
            key: 'userinfo',
            success: function (res) {
              that.setData({
                userinfo: res.data
              })
            },
            fail: function (res) {
              console.info(res.errorMessage);
            }
          });
        }
      },
      fail: function (res) {
        dd.alert({ content: 'fail' });
      },
      complete: function (res) {
        // dd.alert({content: 'complete'});
      }
    });

  },
  submit(e) {
    var id = e.detail.value.id;
    var url = '';
    if (id == '' || id == null) {
      url = 'http://localhost/api/hr/weekly/add';
    } else {
      url = 'http://localhost/api/hr/weekly/edit?id=' + id;
    }
    dd.httpRequest({
      url: url,
      method: 'POST',
      data: {
        goal: e.detail.value.goal,
        result: e.detail.value.result,
        plan: e.detail.value.plan,
      },
      dataType: 'json',
      success: function (res) {
        dd.alert({ content: '已提交' });
      },
      fail: function (res) {
        dd.alert({ content: 'fail' });
      },
      complete: function (res) {
        // dd.alert({content: 'complete'});
      }
    });
  },

  onReset() { },

  goweek(event) {
    var id = event.target.dataset.id;
    console.log('id', id);
    if (id == '' || id == null) {
      this.setData({
        view: 'submit',
        id: '',
        goal: '',
        result: '',
        plan: '',
      })
    } else {
      var url = 'http://localhost/api/hr/weekly/selectListById/' + id;
      var that = this;
      dd.httpRequest({
        url: url,
        method: 'POST',
        data: {
        },
        dataType: 'json',
        success: function (res) {
          that.setData({
            view: 'modify',
            id: res.data.id,
            goal: res.data.goal,
            result: res.data.result,
            plan: res.data.plan,
          })
        },
        fail: function (res) {
          dd.alert({ content: 'fail' });
        },
        complete: function (res) {
          // dd.alert({content: 'complete'});
        }
      });
    }

  }
});
